package javase.javase02.Annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author: Gan 网络编程
 * @date 2021/4/26 0026 18:52
 * @description:Field
 **/
public class TestAnnotation {

   /* class TestAn;

    public TestAnnotation() {
        this.TestAn = TestAnnotation.class;
    }
*/
    @MyAnnotation(name = "孙悟空", abilityNum = 2,
            abilityNames = {"七十二变，火眼金睛"})
    public void people1(String name, int age) {
    }

    @MyAnnotation(name = "猪八戒", abilityNum = 1,
            abilityNames = {"九齿钉耙"})
    public void people2(String name, int age) {
    }

    //使用默认值，无任何配置
    @MyAnnotation
    public void people3(String name, int age) {
    }

    public static void main(String[] args) {
        //得到TestAnnotation中所有成员变量
        Field[] fields = TestAnnotation.class.getDeclaredFields();
        for (Field f : fields) {
            System.out.print(f + " ");
        }

        //反射获取所有方法
        Method[] methods = TestAnnotation.class.getMethods();
        for (Method method : methods) {
            //判断方法是否存在注解
            if (method.isAnnotationPresent(MyAnnotation.class)) {
                MyAnnotation myAnn = method.getAnnotation(MyAnnotation.class);
                System.out.print(myAnn.name() + "有" + myAnn.abilityNum() + "个技能:");
                for (String a : myAnn.abilityNames()) {
                    System.out.print(a + " ");
                }
                System.out.println();
            }
        }
    }
}
